﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace TranslationProjectBuilder.Windows.Forms
{
    public partial class FileTreeView : UserControl
    {
        public FileTreeView()
        {
            InitializeComponent();
        }

        String path;
        public String Path
        {
            get { return path; }
            set { path = value; }
        }

        String key;
        public String Key
        {
            get { return key; }
            set { key = value; }
        }

        public TreeNode SelectedNode
        {
            get { return treeView.SelectedNode; }
        }

        public void loadContent(String path, String[] keys)
        {
            ListDirectory(path, keys);
        }

        private void ListDirectory(String path, String[] searchPattern)
        {
            treeView.Nodes.Clear();

            var rootDirectoryInfo = new DirectoryInfo(path);

            foreach (TreeNode node in CreateDirectoryNode(rootDirectoryInfo, searchPattern).Nodes)
            {
                treeView.Nodes.Add(node);
            }

            treeView.Sort();
        }

        private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo, String[] searchPattern)
        {
            var directoryNode = new TreeNode(directoryInfo.Name);
            foreach (var directory in directoryInfo.GetDirectories())
            {
                directoryNode.Nodes.Add(CreateDirectoryNode(directory, searchPattern));
            }

            foreach (string pattern in searchPattern)
            {
                foreach (var file in directoryInfo.GetFiles(pattern))
                {
                    TreeNode fileNode = new TreeNode(file.Name);
                    fileNode.Name = fileNode.Text;
                    directoryNode.Nodes.Add(fileNode);
                }
            }

            return directoryNode;
        }



        public TreeNode getSelectedNode()
        {
            return treeView.SelectedNode;
        }

        public TreeNodeCollection getNodes()
        {
            return treeView.Nodes;
        }

        public void removeNode()
        {
            if (treeView.SelectedNode != null)
            {
                treeView.SelectedNode.Remove();
            }
        }

        public void insertNode(ref TreeNode node)
        {
            TreeNode parent = node.Parent;
            TreeNode rootNode = createRootNode(node, ref parent);

            insertNode(treeView.Nodes, rootNode);
        }

        private TreeNode createRootNode(TreeNode node, ref TreeNode parentNode)
        {
            if (parentNode == null)
            {
                return (TreeNode)node.Clone();
            }
            else
            {
                TreeNode newRootNode = new TreeNode(parentNode.Text);
                newRootNode.Nodes.Add((TreeNode)node.Clone());
                TreeNode parent = parentNode.Parent;

                return (TreeNode)createRootNode(newRootNode, ref parent).Clone();
            }

        }

        private void insertNode(TreeNodeCollection tnCollection, TreeNode node)
        {
            bool foundMatch = false;

            foreach (TreeNode currentNode in tnCollection)
            {
                if (currentNode.Text == node.Text)
                {
                    foundMatch = true;
                    foreach (TreeNode insertNodes in node.Nodes)
                    {
                        insertNode(currentNode.Nodes, insertNodes);
                    }
                }
            }

            if (!foundMatch)
            {
                tnCollection.Add((TreeNode)node.Clone());
            }
        }
    }
}
